package com.example.yiwei.crm.view.activity.contactActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.customerDao;
import com.example.yiwei.crm.data.GetContact;
import com.example.yiwei.crm.data.GetContactData;
import com.example.yiwei.crm.dao_impl.customerDaoImpl;
import com.example.yiwei.crm.entity.ContactMessageEntity;
import com.example.yiwei.crm.view.Class.listviewClass.AutoListView;

import java.util.ArrayList;
import java.util.List;

public class ContactSimpleMessageActivity extends AppCompatActivity implements AutoListView.OnRefreshListener  {
    private AutoListView lstv;
    MyAdapter adapter;
    List<ContactMessageEntity> contact;
    List<ContactMessageEntity> contactlist;
    ImageView btn_add,btn_back,btn_search;
    Intent intent;
    customerDao customerdao;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            List<ContactMessageEntity> result = (List<ContactMessageEntity>) msg.obj;
            switch (msg.what) {
                case AutoListView.REFRESH:
                    lstv.onRefreshComplete();
                    contact.clear();
                    contact.addAll(result);
                    break;
                case AutoListView.LOAD:
                    lstv.onLoadComplete();
                    contact.addAll(result);
                    break;
            }
//            lstv.setResultSize(result.size());
            adapter.notifyDataSetChanged();

        }

        ;
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crmcontact_simple_message);
        contact = new ArrayList<>();
        btn_add= (ImageView) findViewById(R.id.contactSimpleMessage_btn_plus);
        btn_back= (ImageView) findViewById(R.id.contactSimpleMessage_btn_back);
        btn_search= (ImageView) findViewById(R.id.contactSimpleMessage_btn_search);
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(ContactSimpleMessageActivity.this,ContactSearchActivity.class);
                startActivity(intent);
            }
        });
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GetContact.create();
                Intent intent=new Intent(ContactSimpleMessageActivity.this,ContactAddMessageActivity.class);
                startActivity(intent);

            }
        });
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        lstv= (AutoListView) findViewById(R.id.contact_listview);
        lstv.setDivider(new ColorDrawable(Color.rgb(216,216,216)));
        lstv.setDividerHeight(1);
        adapter = new MyAdapter();
        lstv.setAdapter(adapter);
        lstv.setOnRefreshListener(this);
        lstv.setLoadEnable(false);
        new Thread(new Runnable() {
            @Override
            public void run() {
                 customerdao=new customerDaoImpl();
                contact=customerdao.queryContactAllMessage(baseClass.queryall_contact_message_url,null);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                        lstv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                final int contact_id=contact.get(contact.size()-i).getContact_id();
                                intent=new Intent(ContactSimpleMessageActivity.this,ContactDetaledMessageActivity.class);
                                intent.putExtra("id",contact_id);
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        customerDao customerdao=new customerDaoImpl();
                                        GetContact.contact= customerdao.queryContactMessage(baseClass.query_contact_message_url+contact_id, null);
                                        if ("success".equals(GetContact.contact.getTag())){
                                            startActivity(intent);

                                        }else{
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    Toast.makeText(ContactSimpleMessageActivity.this, "数据加载失败", Toast.LENGTH_SHORT).show();
                                                }
                                            });
                                        }

                                    }
                                }).start();
                            }
                        });
                    }
                });
            }
        }).start();
    }
    protected void onRestart() {
        super.onRestart();
        if (GetContactData.contacts!=null){
            contact.clear();
            contact.addAll(GetContactData.contacts);
            adapter.notifyDataSetChanged();
            GetContactData.contacts=null;
        }

    }
    private void initData() {
        loadData(AutoListView.REFRESH);
    }
    private void loadData(int refresh) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(700);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Message msg = handler.obtainMessage();
                int what = 0;
                msg.what = what;
                msg.obj = getData();
                if (msg.obj!=null) {
                    handler.sendMessage(msg);
                }else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(ContactSimpleMessageActivity.this, "网络不给力啊！请重试", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();
    }
    @Override
    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }
    public List<ContactMessageEntity> getData() {
        contactlist = null;
        new Thread(new Runnable() {
            @Override
            public void run() {
                customerDao contactdao = new customerDaoImpl();
                contactlist = contactdao.queryContactAllMessage(baseClass.queryall_contact_message_url, null);

            }
        }).start();
        if (contactlist!=null){
            return contactlist;
        }else{
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return contactlist;
        }
    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return   contact.size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view==null){
                view=getLayoutInflater().inflate(R.layout.listview_contact,null);
                holder=new ViewHolder();
                holder.txt_name= (TextView) view.findViewById(R.id.listview_contact_txt_name);
                holder.txt_phone= (TextView) view.findViewById(R.id.listview_contact_txt_mobile_phone);
                view.setTag(holder);
            }else{
                holder= (ViewHolder) view.getTag();
            }
            holder.txt_name.setText(contact.get(contact.size()-i-1).getContact_name());
            holder.txt_phone.setText(contact.get(contact.size()-i-1).getContact_mobile_phone());
            return view;
        }
        class ViewHolder{
            TextView txt_name,txt_phone;
        }
    }
}
